home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14088 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ANSI C and POSIX (was Re: C/C++ knocks the crap out of Ada)
  5. Date: 11 Apr 1996 11:27:04 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4kjitoINNh41@anvil.ugrad.cs.ubc.ca>
  8. References: <JSA.96Feb16135027@organon.com> <829066525snz@genesis.demon.co.uk> <dewar.829096975@schonberg> <yxspw9eommk.fsf@stint.cl.cam.ac.uk>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <yxspw9eommk.fsf@stint.cl.cam.ac.uk>,
  12. Gareth Rees <gdr11@cl.cam.ac.uk> wrote:
  13.  >Robert Dewar <dewar@cs.nyu.edu> wrote:
  14.  >> Highly imprecise thinking here I fear. There is no practical way for
  15.  >> any implementation to do the check you mention (this = value in 3rd
  16.  >> argument that is greater than the available buffer size).
  17.  >
  18.  >But the *programmer* can do the check while writing the program!  The
  19.  >programmer must have declared or allocated a buffer of some size.
  20.  >Consider: if I wrote code like
  21.  >
  22.  >    #include<stdio.h>
  23.  >
  24.  >    int main () {
  25.  >        char buf[80];
  26.  >        /* Input lines are always 78 characters or less, so the limit of
  27.  >           100 can never cause a buffer overflow */
  28.  >        while (fgets(buf,100,stdin))
  29.  >            puts(buf);
  30.  >        return 0;
  31.  >    }
  32.  >
  33.  >you would quite rightly point out that this is gratuitously broken and
  34.  >that it would cost me nothing (and gain a lot) to change `100' to `80'.
  35.  >It's the same with the buffer size in `read'.  Why make the byte count
  36.  >larger than the buffer size when you don't have to?
  37.  
  38. Because only careful rationality can convince you not to it, not the vague
  39. POSIX specification for the read function.
  40.  
  41. What about the select() call? Would you assume that the timeval structure is
  42. modified by select() if every system you ever used select() with left the
  43. structure alone?  This is not as clear-cut as the read() case. 
  44.  
  45. On some systems, the parameter is even declared as ``const struct timeval
  46. *timeout'', which is a promise that it won't me modified. Yet under Linux it
  47. _is_ modified.
  48.  
  49. It's safer to write code that assumes the structure will be modified, but I
  50. would not have even thought of it until coming to Linux.
  51.  
  52. To this date, I have personally ``fixed'' two or three programs because of this
  53. difference, the most recent of which was the old BSD game ``hunt''.
  54.  
  55. It comes with Slackware, compiled for AF_UNIX sockets---unlike me who was
  56. desperate for a good match with some buddies, they apparently just gave up
  57. getting it work properly with AF_INET. Due to the select() difference, the
  58. client would not wait enough for a server reply over a slow network, since the
  59. game would use the same struct timeval each time select() was called without
  60. re-initializing its contents.
  61. -- 
  62.  
  63.